<?php

namespace model;

use \common\Check;

class feedbacks
{
    const TABLE = "feedbacks";

    private static function db($table = null)
    {
        return \libs\db::Init($table);
    }
    //查找一条数据
    public static function findData()
    {
        $where['id'] = ROUTE['query']['id'];
        return self::db(self::TABLE)->where($where)->find();
    }
    //查询列表
    public static function selectData()
    {
        $page = ['num' => 25, 'p' => ROUTE['query']['p'], 'return' => ['prev', 'next', 'first', 'last', 'list']];
        return [
            'data' => self::db(self::TABLE)->where(['status' => 1])->page($page)->order('id DESC')->select(),
            'page' => self::db(self::TABLE)->getPage()
        ];
    }

    //添加数据
    public static function insertData()
    {
        $data = [
            'title' => VarFilter($_POST['title']),
            'name' => VarFilter($_POST['name']),
            'phone' => VarFilter($_POST['phone']),
            'content' => VarFilter($_POST['content']),
            'mid' => $_POST['mid'],
            'ip' => GetIp(),
            'time' => date('Y-m-d H:i:s', time()),
        ];
        //验证数据
        $msg = Check::rule(
            array(check::len($data['title'], 5, 50), '标题为5-50个字符，请检查'),
            array(check::mobile($data['phone']), '手机号码不对，请检查'),
        );
        if ($msg !== true) {
            return $result = ['status' => 0, 'msg' => $msg];
        } else {
            $re_key = self::db(self::TABLE)->insert($data);
            if ($re_key) {
                $result = ['key' => $re_key, 'status' => 1, 'msg' => '<b style="color:red">成功提交！</b><br>您现在还不能看到您写的内容！<br>您提交的信息将由系统后台审核后才能看到，请耐心等待！'];
            } else {
                $result = ['status' => 0, 'msg' => $re_key];
            }
            return $result;
        }
    }
}
